/*
 * @ProjectName: 编程学习
 * @Copyright:   2019 HangZhou Yi Dev, Ltd. All Right Reserved.
 * @address:     https://yiyuery.github.io/
 * @date:        2019/12/9 8:26 下午
 * @email:       xiazhaoyang@live.com
 * @description: 本内容仅限于编程技术学习使用，转发请注明出处.
 */
package com.example.spring.aop.monitor;

import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

/**
 * <p>
 * 业务方式执行监视器
 * </p>
 *
 * @author Yi
 * @version V1.0.0
 * @date 2019/12/9 8:26 下午
 * @modificationHistory=========================逻辑或功能性重大变更记录
 * @modify By: {修改人}  2019/12/9
 * @modify reason: {方法名}:{原因}
 * ...
 */
@Slf4j
public class BusinessLogMonitor {

    /**
     * 线程同步（缓存监视器）
     */
    private static ThreadLocal<BusinessLogHandler> handlerThreadLocal = new ThreadLocal<>();

    /**
     * 初始化业务日志处理器
     *
     * @param method
     */
    public static void begin(String method) {
        log.info("> " + method + ": begin monitor...");
        BusinessLogHandler handler = new BusinessLogHandler(method);
        handlerThreadLocal.set(handler);
    }

    /**
     * 结束并打印业务日志
     */
    public static void end() {
        BusinessLogHandler handler = handlerThreadLocal.get();
        if(Objects.isNull(handler)){
            log.info(">" + handler.getServiceMethod() + ":end monitor....");
            handler.businessLog();
            handlerThreadLocal.remove();
    }else{
        //增加判空逻辑，便于测试后置增强
        log.info("> end monitor....");
    }

    }
}
